jkmegamenu.definemenu("megaanchor", "megamenu1", "click")
var currentResView = null;
var initializedTree = {};
function setContent() {
	var windowHeight = getWindowHeight();
	$("#tree_div").css("height", windowHeight - 31);
	var contentElement = document.getElementById('contentFrame');
	contentElement.style.height = (windowHeight - 29) + 'px';
	contentElement.style.width = (getWindowWidth() - 200) + 'px';
}
function configTree(viewId) {
	var setting = null;
	
	if (viewId == 1) {
		setting = {
			async : {
				enable : true,
				url : "application-tree.action",
				autoParam : ["id"],
				otherParam : ["viewId", viewId]
			},
			view : {
				expandSpeed : ""
			},
			callback : {
				onClick : function(event, treeId, treeNode) {
					nodeId = treeNode.id;
					var arr = nodeId.split("|");
					lastNode.type = arr[0];
					lastNode.id = arr[1];
					lastNode.pId = arr[2];
					var treeObj = $.fn.zTree.getZTreeObj(treeId);
					if (lastNode.type == 3) {
						treeObj.expandNode(treeNode, true, false, true);
						treeNode = treeNode.children[0];
						nodeId = treeNode.id;
						treeObj.selectNode(treeNode);
						var arr = nodeId.split("|");
						lastNode.type = arr[0];
						lastNode.id = arr[1];
						lastNode.pId = arr[2];
					}
					callLastNodeMenu();
					saveLastResNode(treeNode);
				}
			}
		};
	} else if (viewId == 2) {
		setting = {
			async : {
				enable : true,
				url : "motitype-tree.action",
				autoParam : ["id"],
				otherParam : ["viewId", viewId]
			},
			view : {
				expandSpeed : ""
			},
			callback : {
				onClick : function(event, treeId, treeNode) {
					nodeId = treeNode.id;
					var arr = nodeId.split("|");
					lastNode.type = arr[0];
					lastNode.id = arr[1];
					lastNode.pId = arr[2];
					var treeObj = $.fn.zTree.getZTreeObj(treeId);
					if (lastNode.type == 3) {
						treeObj.expandNode(treeNode, true, false, true);
						treeNode = treeNode.children[0];
						nodeId = treeNode.id;
						treeObj.selectNode(treeNode);
						var arr = nodeId.split("|");
						lastNode.type = arr[0];
						lastNode.id = arr[1];
						lastNode.pId = arr[2];
					}
					callLastNodeMenu();
					saveLastResNode(treeNode);
				}
			}
		};
	} else {
		setting = {
			async : {
				enable : true,
				url : "res-tree.action",
				dataType : "text",
				autoParam : ["id"],
				otherParam : ["viewId", viewId]
			},
			view : {
				expandSpeed : ""
			},
			callback : {
				onClick : function(event, treeId, treeNode) {
					nodeId = treeNode.id;
					var arr = nodeId.split("|");
					lastNode.type = arr[0];
					lastNode.id = arr[1];
					lastNode.pId = arr[2];
					var treeObj = $.fn.zTree.getZTreeObj(treeId);
					if (lastNode.type == 3) {
						treeObj.expandNode(treeNode, true, false, true);
						treeNode = treeNode.children[0];
						nodeId = treeNode.id;
						treeObj.selectNode(treeNode);
						nodeId = treeNode.id;
						var arr = nodeId.split("|");
						lastNode.type = arr[0];
						lastNode.id = arr[1];
						lastNode.pId = arr[2];
					}
					callLastNodeMenu();
					saveLastResNode(treeNode);
				}
			}
		};
	}
	return setting;
}
function saveLastResNode(treeNode) {
	nodeId = treeNode.id;
	var arr = nodeId.split("|");
	lastNode.type = arr[0];
	lastNode.id = arr[1];
	lastNode.pId = arr[2];
	var pNode = treeNode.getParentNode();
	var record = new Array();
	record.push(treeNode.id);
	while (pNode != null) {
		record.push(pNode.id);
		pNode = pNode.getParentNode();
	}

	$.post("res-frame!saveNodeTrace.action", "nodes=" + record + "&viewId="
					+ currentResView);
}

function disabledMenu(resId) {
	var flag;
	$.ajax({
				url : 'res-frame!hasResManagePrivilege.action?resId=' + resId,
				cache : false,
				global : false,
				async : false,
				success : function(hasprivilege) {
					if (hasprivilege == 'true') {
						flag = false;
					} else {
						flag = true;
					}
				}
			});
	$("#add_res").attr("disabled", flag);
	$("#find_res").attr("disabled", flag);
	$("#manage_res_rel").attr("disabled", flag);
	$("#res-custom-frame").attr("disabled", flag);

}
var lastNode = new Object();
var menuMap = {};
var lastMenu = {};
lastMenu.res = "res_info";
lastMenu.resGroup = "group_info";
function menuClick() {
	$(".actived").removeClass("actived");
	$(this).addClass("actived");
	if (menuMap[this.name] == null) {
		menuMap[this.name] = this.href;
	}
	if (lastNode.type == 2) {
		lastMenu.res = this.id;
	} else {
		lastMenu.resGroup = this.id;
	}
	
	var arg = "?id=" + lastNode.id + "&pId=" + lastNode.pId + "&viewId="
			+ currentResView + "&ntype=" + lastNode.type;
	$(this).attr("href", menuMap[this.name] + arg);
	$.post("res-frame!saveCurrentMenu.action", "menuId=" + $(this).attr("name")
					+ "&type=" + lastNode.type);
}
function viewClick(e) {
	$("#tree_" + currentResView).hide();
	timeid = refreshMap['tree_' + currentResView];
	clearInterval(timeid);
	var resView = e.data.view;
	currentResView = resView.id;
	$("#captionA").html(resView.name);
	showTree(resView.id);
	timeid = setInterval('refreshData(' + resView.id + ')', 15000);
	refreshMap['tree_' + resView.id] = timeid;

	if (currentResView >3 && initResMap[currentResView] == null) {
		$("#resMenu").hide();
		var url = "res!guide.action?viewId=" + currentResView;
		$("#add_top_res").attr("href", url);
		document.getElementById("add_top_res").click();
	}

	$.post("res-frame!saveCurrentView.action", "viewId=" + resView.id);
}
function appendResView(resView) {
	$("#menu").append("<li><a href='#' id='view_" + resView.id + "'>"
			+ resView.name + "</a></li>");
	$("#tree_div")
			.append("<ul id='tree_"
					+ resView.id
					+ "' class='ztree' style='margin:0px; padding:0px; display:none;'></ul>");
	$("#view_" + resView.id).bind('click', {
				view : resView
			}, viewClick);
}
function initFirstView(resView) {
	currentResView = resView.id;
	$("#captionA").html(resView.name);
	showTree(currentResView);
	timeid = setInterval('refreshData(' + resView.id + ')', 15000);
	refreshMap['tree_' + resView.id] = timeid;

}

function focusTreeNode(viewId, times) {
	var treeObj = $.fn.zTree.getZTreeObj("tree_" + viewId);
	try {
		var isSelected = true;
		if (lastNodeTrace != '' && lastNodeTrace[viewId] != null) {
			var list = lastNodeTrace[viewId];
			if (list != null) {
				var node = treeObj.getNodeByParam("id", list[list.length - 1],
						null);
				if (node == null)
					isSelected = false;
				treeObj.selectNode(node, false);
			} else {
				isSelected = false;
			}

		} else {
			var roots = treeObj.getNodes();
			if (roots != null && roots.length > 0) {
				treeObj.selectNode(roots[0], false);
			} else {
				isSelected = false;
			}
		}
		if (isSelected) {
			if (lastNodeTrace[viewId] == null) {
				// 此时树已加载
				var nodes = treeObj.getSelectedNodes();
				var arr = nodes[0].id.split("|");
				lastNode.type = arr[0];
				lastNode.id = arr[1];
				lastNode.pId = arr[2];
				callLastNodeMenu();
			}
		} else {
			if (times >= 20) {
				return;
			}
			setTimeout("focusTreeNode('" + viewId + "'," + (times + 1) + ")",
					100);
		}

	} catch (e) {
	}
}
function callLastNodeMenu() {
	if (lastNode.type == 2) {
		$("#resMenu").show();
		$("#groupMenu").hide();
		document.getElementById(lastMenu.res).click();
	} else if (lastNode.type == 4 || lastNode.type == 5) {
		$("#groupMenu").show();
		$("#resMenu").hide();
		document.getElementById(lastMenu.resGroup).click();
	}
}
function showTree(viewId) {
	$("#tree_" + viewId).show();
	if (!initializedTree[viewId]) {
		// 初始化时
		$.fn.zTree.init($("#tree_" + viewId), configTree(viewId));
		initializedTree[viewId] = true;
		setTimeout("focusTreeNode('" + viewId + "',1)", 200);
		var list = lastNodeTrace[currentResView];
		if (list != null) {
			var arr = list[list.length - 1].split("|");
			lastNode.type = arr[0];
			lastNode.id = arr[1];
			lastNode.pId = arr[2];
		}
	} else {
		var treeObj = $.fn.zTree.getZTreeObj("tree_" + viewId);
		var nodes = treeObj.getSelectedNodes();
		if (nodes != null && nodes.length > 0) {
			nodeId = nodes[0].id;
			var arr = nodeId.split("|");
			lastNode.type = arr[0];
			lastNode.id = arr[1];
			lastNode.pId = arr[2];
		} else {
			var roots = treeObj.getNodes();
			if (roots != null && roots.length > 0) {
				treeObj.selectNode(roots[0], false);
			}
		}

	}

	// if (lastNodeTrace[viewId] != null) {
	callLastNodeMenu();
	// }

}
getResTree = function() {
	return $.fn.zTree.getZTreeObj("tree_" + currentResView);
}
refreshTree = function() {
	var tree = $.fn.zTree.getZTreeObj("tree_" + currentResView);
	tree.reAsyncChildNodes(null, "refresh");
}
deleteSelectedNode = function() {
	var tree = $.fn.zTree.getZTreeObj("tree_" + currentResView);
	var selectedNode = tree.getSelectedNodes()[0];
	var movedNode = null;
	if ((movedNode = selectedNode.getNextNode()) != null) {
	} else if ((movedNode = selectedNode.getPreNode()) != null) {
	} else if ((movedNode = selectedNode.getParentNode()) != null) {
	}
	var typeremoved = false;
	var typenode = null;
	try {
		if (movedNode.iconSkin.indexOf("res") == -1) {
			tree.expandNode(movedNode, true, false, true);
			typeremoved = movedNode.children.length == 1 ? true : false;
			if (movedNode.iconSkin.indexOf("type") != -1)
				typenode = movedNode;
		}
		if (typeremoved) {
			movedNode = movedNode.getParentNode();
		}
		tid = movedNode.tId + "_a";
		document.getElementById(tid).click();
	} finally {
		tree.removeNode(selectedNode);
		if (typenode != null) {
			tree.removeNode(typenode);
		}
		tree.selectNode(movedNode);
	}

}
var refreshMap = new Object();
refreshData = function(viewId) {
	var url = "res-tree!refreshTreeNode.action";
	if (viewId == 2) {
		url = "motitype-tree!refreshTreeNode.action";
	}else if (viewId == 1) {
		url = "application-tree!refreshTreeNode.action";
	}
	$.post(url, 'viewId=' + viewId, function(results) {
				var treeObj = $.fn.zTree.getZTreeObj('tree_' + viewId);
				items = results[0];
				for (var i = 0; i < items.length; i++) {
					var key = items[i][0];
					var newstatus = items[i][1];
					var node = treeObj.getNodeByParam("id", key, null);
					if (node != null) {
						node.iconSkin = newstatus;
						treeObj.updateNode(node);
					}
				}
				items = results[1];
				for (var i = 0; i < items.length; i++) {
					var key = items[i][0];
					var newname = items[i][1];
					var node = treeObj.getNodeByParam("id", key, null);
					if (node != null) {
						node.name = newname;
						treeObj.updateNode(node);
					}
				}
			});
}

var lastNodeTrace = "";
var initResMap = null;
function initResView() {
	$.ajax({
				url : "res-frame!initView.action",
				cache : false,
				global : false,
				async : false,
				dataType : 'json',
				success : function(data) {
					try {
							
						var lastResMenu = data[2];
						lastNodeTrace = data[4];
				
						$("#" + lastResMenu).addClass("actived");
					
						lastMenu.res = lastResMenu;
						
						var resViews = data[0];
					
						for (var i = 0; i < resViews.length; i++) {
							appendResView(resViews[i]);
						}
						initResMap = data[3];
						initFirstView(data[1]);
						if (data[1].id >3
								&& initResMap[currentResView] == null) {
								
							$("#resMenu").hide();
							var url = "res!guide.action?viewId="
									+ currentResView;
							$("#add_top_res").attr("href", url);
							document.getElementById("add_top_res").click();
						}
					} catch (e) {
						alert(e.description);
					} finally {
						$('body').unmask();
					}
				}
			});

}
$(document).ready(function() {
	innerLayout = $('#outer-center').layout({
				center__paneSelector : ".inner-center",
				north__paneSelector : ".inner-north",
				north__size : 29,
				spacing_open : 0,
				spacing_closed : 0
			});
	outerLayout = $('body').layout({
				center__paneSelector : ".outer-center",
				west__paneSelector : ".outer-west",
				west__size : 190,
				slidable : false,
				spacing_open : 8 // ALL panes
				,
				spacing_closed : 8 // ALL panes
				,
				onopen_end : function() {
					var windowHeight = getWindowHeight();
					var contentElement = document
							.getElementById('contentFrame');
					contentElement.style.height = (windowHeight - 38) + 'px';
					contentElement.style.width = (getWindowWidth() - 200)
							+ 'px';
				},
				onclose_end : function() {
					var windowHeight = getWindowHeight();
					var contentElement = document
							.getElementById('contentFrame');
					contentElement.style.height = (windowHeight - 38) + 'px';
					contentElement.style.width = (getWindowWidth() - 8) + 'px';
				}
			});
	$('body').mask("加载中...");
	$("#resMenu a").each(function() {
				$(this).bind('click', menuClick);
			});
	$("#groupMenu a").each(function() {
				$(this).bind('click', menuClick);
			});
	initResView();
	document.getElementById('menu').style.height = (getWindowHeight() - 38)
			+ 'px';
});